import React from 'react'
import { Button } from 'antd';
import BasicInfo from 'pages/servicelist/basicInfo.jsx'
import ItemInfo from 'pages/servicelist/itemInfo.jsx'

import StateMixin from 'reflux-state-mixin';
import SrvListAction from 'pages/servicelist/actions/serviceListAction.js'
import SrvListStore from 'pages/servicelist/stores/serviceListStore.js'

export default React.createClass({
  mixins: [StateMixin.connect(SrvListStore)],
  getInitialState() {
    return {
      iconName: 'alter',
      fileId:null
    };
  },
  changeIcon(name) {
    this.setState({
      iconName:name
    })
  },
  savefileId(fileId) {
    this.setState({ fileId })
  },
  removeIcon() {
  },

  saveSrvCatId(srvId) {
    this.props.saveSrvCatId(srvId);
  },

  handleReset(e) {
    e.preventDefault();
    this.refs.basicInfo.resetFields();
    this.refs.itemInfo.resetFields();
  },
  handleOk() {
    // log(this.refs.itemInfo.fieldsMeta.srvItems.initialValue)
    this.refs.basicInfo.validateFields((errors, values) => {
      if (errors) {
        return;
      }
      this.setState({
        selectedRows:this.refs.itemInfo.fieldsMeta.srvItems.initialValue
      })
      // let itemInfoFields = this.state.selectedRows;

      let items = [];
      _.map(this.refs.itemInfo.fieldsMeta.srvItems.initialValue, (item) => {
        let arr = {id:item.id};
        items.push(arr)
      })

      let data = {
        id:this.props.srvCatIde,
        title: values.title,
        authorName: values.authorName,
        description: values.description,
        srvItems:items
      }
      // log(data)
      SrvListAction.updateSrvCatDir(data).then(() => {})
    })
  },


  render() {    
    return (
      <div className="config-main" >
        <BasicInfo ref="basicInfo" savefileId={this.savefileId}
          changeIcon={this.changeIcon} iconName={this.state.iconName}
          fileId={this.state.fileId} removeIcon={this.removeIcon}/>
        <ItemInfo ref="itemInfo" savefileId={this.savefileId}
          changeIcon={this.changeIcon} iconName={this.state.iconName}
          fileId={this.state.fileId} removeIcon={this.removeIcon}/>
        <div className="action">
          <Button type="primary" onClick={this.handleOk} >
            保存
          </Button>
          <Button type="primary" onClick={this.handleReset}>
            重置
          </Button>
        </div>
      </div>
    );
  }
});

